package com.tianji.learning.service.impl;

import com.tianji.common.utils.BeanUtils;
import com.tianji.common.utils.CollUtils;
import com.tianji.learning.entity.po.PointsBoardSeason;
import com.tianji.learning.entity.vo.PointsBoardSeasonVO;
import com.tianji.learning.mapper.PointsBoardSeasonMapper;
import com.tianji.learning.service.IPointsBoardSeasonService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author lemon
 */
@Slf4j
@Service
public class PointsBoardSeasonServiceImpl extends ServiceImpl<PointsBoardSeasonMapper, PointsBoardSeason> implements IPointsBoardSeasonService {

    /**
     * 历史赛季
     *
     * @return
     */
    @Override
    public List<PointsBoardSeasonVO> queryAllSeason() {
        List<PointsBoardSeason> list = list();
        // 封装vo
        if (CollUtils.isEmpty(list)) {
            return CollUtils.emptyList();
        }
        return list.stream().map(item -> {
            return BeanUtils.copyBean(item, PointsBoardSeasonVO.class);
        }).collect(Collectors.toList());
    }

    /**
     * 根据时间查询赛季id
     *
     * @param time
     * @return
     */
    @Override
    public Integer querySeasonByTime(LocalDate time) {
        // start_time <= time and end_time >= time
        PointsBoardSeason one = this.lambdaQuery()
                .le(PointsBoardSeason::getBeginTime, time)
                .ge(PointsBoardSeason::getEndTime, time)
                .one();
        return one != null ? one.getId() : null;
    }

    @Override
    public PointsBoardSeason queryPointsSeasonByTime(LocalDate now) {
        return this.lambdaQuery()
                .le(PointsBoardSeason::getBeginTime, now)
                .ge(PointsBoardSeason::getEndTime, now)
                .one();
    }

    /**
     * 添加新的赛季
     *
     * @param boardSeason
     */
    @Override
    public void addNewSeason(PointsBoardSeason boardSeason) {
        this.save(boardSeason);
    }

    /**
     * 根据名称查询赛季
     *
     * @param name
     * @return
     */
    @Override
    public PointsBoardSeason queryPointsSeasonByName(String name) {

        return this.lambdaQuery()
                .eq(PointsBoardSeason::getName, name)
                .one();
    }
}
